﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.DRV.Drv
{
    public class OmronPlcSocketDrv
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(OmronPlcSocketDrv));
        Socket socket;
        string IP;
        int Port;

        public OmronPlcSocketDrv(string ip,int port)
        {
            this.IP = ip;
            this.Port = port;
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SendTimeout = 3000;
            ReceiveTimeout = 3000;
            socket.SendTimeout = SendTimeout;
            socket.ReceiveTimeout = ReceiveTimeout;
            TcpClient a; 
        }

        public bool Connected
        {
            get
            {
                try
                {
                    Ping ping = new Ping();
                    var a = ping.Send(IP); 
                    if (a.Status == IPStatus.Success&& socket.Connected)
                    {
                        Console.WriteLine("Status:{0} Connected:{1}", a.Status, socket.Connected);
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("Status:{0} Connected:{1}", a.Status, socket.Connected);
                        return false;
                    }
                }
                catch
                {
                    return false;
                }               
            }
        }

        public void Connect()
        {
            socket.Connect(IP, Port);
        }

        public void Disconnect()
        {
            try
            {
                if (!socket.Connected)
                    return; 
                socket.Close();

                log.Info(string.Format("关闭注液机 {0},{1}", IP, Port));
            }
            catch (Exception ex)
            {
                log.Error(string.Format("关闭注液机 {0},{1} 异常:{2}", IP, Port, ex));
            }
        }

        public void Open()
        {
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
                socket.SendTimeout = SendTimeout;
                socket.ReceiveTimeout = ReceiveTimeout;
                socket.Connect(IP, Port);
                log.Info(string.Format("打开注液机 {0},{1}", IP, Port));
            }
            catch (Exception ex)
            {
                log.Error(string.Format("打开注液机 {0},{1} 发生异常 {2} ：", IP, Port, ex));
            }
        } 

        public string Read()
        {
            try
            {
                var buff = new byte[10240];
                var len = socket.Receive(buff);
                var result = Encoding.Default.GetString(buff, 0, len);                
                return result;
            }
            catch (Exception ex)
            {
                log.Error(string.Format("注液机 {0},{1} 读发生异常：{2}", IP, Port, ex));
                return "";
            }
        }

        public int Write(string s)
        { 
           var sendCmd= Encoding.Default.GetBytes(s);
            try
            {
                var len = socket.Send(sendCmd);
               // log.Error(string.Format("注液机 {0},{1} 写完成 {2}", IP, Port, s));
                return len;
            }
            catch (Exception ex)
            {
                log.Error(string.Format("注液机 {0},{1} 写发生异常：{2}", IP, Port, ex));
                return 0;
            }
        }

        int receiveTimeout;
        public int ReceiveTimeout
        {
            get { return receiveTimeout; }
            set
            {
                receiveTimeout = value;
            }
        }
        int sendTimeout;
        public int SendTimeout
        {
            get { return sendTimeout; }
            set
            {
                sendTimeout = value;
            }
        }
    }
}
